<?php

namespace application\vote\models;

class vote_option_model {

    /**
     * 说明:添加投票选项操作
     * @param $data 选项数组
     * @param $subjectid 投票标题ID
     */
    public static function add_options($data, $subjectid, $siteid) {
        //判断传递的数据类型是否正确
        if (!is_array($data))
            return FALSE;
        if (!$subjectid)
            return FALSE;
        foreach ($data as $key => $val) {
            if (trim($val) == '')
                continue;
            $newoption = array(
                'subjectid' => $subjectid,
                'siteid' => $siteid,
                'option' => $val,
                'image' => '',
                'listorder' => 0
            );
            db('vote_option')->insert($newoption);
        }
        return TRUE;
    }

    /**
     * 说明:更新选项
     * @param $data 数组  Array ( [44] => 443 [43(optionid)] => 334(option 值) )
     * @param $subjectid
     */
    public static function update_options($data) {
        //判断传递的数据类型是否正确
        if (!is_array($data))
            return FALSE;
        foreach ($data as $key => $val) {
            if (trim($val) == '')
                continue;
            $newoption = array(
                'option' => $val,
            );
            db('vote_option')->update($newoption, array('optionid' => $key));
        }
        return TRUE;
    }

    /**
     * 说明: 查询 该投票的 选项
     * @param $subjectid 投票ID
     */
    public static function get_options($subjectid) {
        if (!$subjectid)
            return FALSE;
        return db('vote_option')->where(array('subjectid' => $subjectid))->select();
    }

    /**
     * 说明:删除单条对应ID的选项记录
     * @param $optionid 投票选项ID
     */
    public static function del_option($optionid) {
        if (!$optionid)
            return FALSE;
        return db('vote_option')->where(array('optionid' => $optionid))->delete();
    }

}
